package com.qt.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.UnicodeUtil;
import com.qt.annotation.OpenApi;
import com.qt.manager.EmailManager;
import com.qt.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;

/**
 * @author pgw
 * @version 1.0
 * @description
 * @date Create by 2023/9/17 22:51
 */
@RestController
@Slf4j
public class MailController {
    @Autowired
    private EmailManager emailManager;

    @Autowired
    private TemplateEngine templateEngine;

    @OpenApi
    @GetMapping("/sendMail")
    public  String sendMail(){
        DateTime expireTime = DateUtil.offsetMinute(new Date(), 10);
        JavaMailSenderImpl mailSender = emailManager.getMailSender();
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,
                    true);
            // 设置渲染到html页面对应的值
            Context context = new Context();
//            WebConfig webConfig = nacosSwitchConfig.getWebConfig();
            context.setVariable(Constants.Email.OJ_NAME.name(), UnicodeUtil.toString("QT"));
            context.setVariable(Constants.Email.OJ_SHORT_NAME.name(), UnicodeUtil.toString("QT"));
//            context.setVariable(Constants.Email.OJ_URL.name(), "47.103.215.76");
            context.setVariable(Constants.Email.EMAIL_BACKGROUND_IMG.name(), "https://foruda.gitee.com/avatar/1677019480347156572/4763416_qingtengstudio_1578973926.png!avatar200");

            String resetUrl;
//            if (webConfig.getBaseUrl().endsWith("/")) {
//                resetUrl = webConfig.getBaseUrl() + "reset-password?username=" + username + "&code=" + code;
//            } else {
//                resetUrl = "47.103.215.76" + "/reset-password?username=" + "user" + "&code=" + "6756";
//            }

//            context.setVariable("RESET_URL", resetUrl);
//            context.setVariable("EXPIRE_TIME", expireTime.toString());
            context.setVariable("USERNAME", "");

            //利用模板引擎加载html文件进行渲染并生成对应的字符串
            String emailContent = templateEngine.process("emailTemplate_resetPassword", context);

            mimeMessageHelper.setSubject(UnicodeUtil.toString("重生之都市仙尊") );

            mimeMessageHelper.setText(emailContent, true);
            // 收件人
            mimeMessageHelper.setTo("3219857569@qq.com");
            mimeMessageHelper.setTo(new String[] {"1851537669@qq.com"});
            // 发送人
            mimeMessageHelper.setFrom(new InternetAddress("3219857569@qq.com"));
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            log.error("用户重置密码的邮件任务发生异常------------>{}", e.getMessage());
        }
//        ukdymwxfgufcdcib
        return "send ok";
    }

}
